<?php

declare(strict_types=1);

namespace App\Controller\Chat;

use App\Controller\BaseController;
use App\Request\Chat\CloseRoomRequest;
use App\Request\Chat\CreateRoomRequest;
use App\Request\Chat\KickMemberRequest;
use App\Request\Chat\MuteMemberRequest;
use App\Request\Chat\SetMemberRoleRequest;
use App\Request\Chat\ShowRoomRequest;
use App\Request\Chat\UpdateRoomRequest;
use App\Services\YunXinCloud\WyyChatRoomService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;

class RoomController extends BaseController
{
    #[Inject]
    protected WyyChatRoomService $service;

    /**
     * Note - 获取聊天室列表
     * DateTime 2024/8/30 12:41
     * User Admin
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function index()
    {
        $params = $this->request->post();
        $data = $this->service->getRoomList($params);
        return $this->return($data);
    }


    /**
     * Note - 创建聊天室
     * DateTime 2024/8/29 19:04
     * User Admin
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function create(CreateRoomRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $userInfo = Context::get('user');
        $data = $this->service->createChatRoom($userInfo, $params);
        return $this->return($data);
    }


    /**
     * Note - 获取聊天室信息
     * DateTime 2024/8/30 14:58
     * User Admin
     * @param ShowRoomRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function show(ShowRoomRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $data = $this->service->showChatRoom($params);
        return $this->return($data);
    }


    /**
     * Note - 更新聊天室信息
     * DateTime 2024/8/30 15:04
     * User Admin
     * @param UpdateRoomRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function update(UpdateRoomRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $data = $this->service->updateChatRoom($params);
        return $this->return($data);
    }


    /**
     * Note - 开放/关闭聊天室
     * DateTime 2024/8/30 15:04
     * User Admin
     */
    public function toggleCloseStat(CloseRoomRequest $request)
    {
        $params = $this->request->post();
        $params['operator'] = $params['uid'];
        $request->execute($params);
        $data = $this->service->toggleCloseStat($params['room_number'], $params['operator'], $params['valid']);
        return $this->return($data);
    }


    /**
     * Note - 设置聊天室成员角色
     * DateTime 2024/8/30 15:41
     * User Admin
     */
    public function setMemberRole(SetMemberRoleRequest $request)
    {
        $params = $this->request->post();
        $params['operator'] = $params['uid'];
        $request->execute($params);
        $data = $this->service->setMemberRole($params);
        return $this->return($data);
    }

    /**
     * Note - 踢出聊天室成员
     * DateTime 2024/9/3 12:30
     * User Admin
     * @param KickMemberRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function kickMember(KickMemberRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params);
        $data = $this->service->kickMember($params);
        if (!$data) {
            return $this->fail();
        }
        return $this->return();

    }


    /**
     * Note - 禁言成员或者禁言房间
     * DateTime 2024/9/3 14:48
     * User Admin
     * @param MuteMemberRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function mute(MuteMemberRequest $request)
    {
        $params = $this->request->post();
        $request->execute($params, 'default');

        if ($params['type'] == 1) {
            $request->execute($params, 'member');
            $this->service->muteMember($params);
        }
        if ($params['type'] == 2) {
            $request->execute($params, 'room');
            $this->service->muteRoom($params);
        }
        return $this->return();

    }

}
